<?php
namespace app\html5\controller;

use app\Db2;
use think\facade\Cache;
use think\response\Json;

class Index
{
    /**
     * @Notes: 接口描述
     * @Interface patient
     * @param $a
     * @param $b
     * @return string
     * @author: Shaco
     * @Time: 2022/8/23-21:32
     */
    public function index()
    {
        $redis = Cache::store('redis');
        // return rejson(20000,'推送成功');
    }

//    public function get_add_user_linshi_key($length = 10){
//        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
//        $string = '';
//        for ($i = 0; $i < $length; $i++) {
//            $string .= $characters[rand(0, strlen($characters) - 1)];
//        }
//
//        $data['linshi_key'] = substr(sha1($string), 0, $length);
////        return substr(sha1($string), 0, $length);
//
//        $redis = Cache::store('redis');
//        $redis->set('LINSHI_KEY:'.$data['linshi_key'], 1,60*60*24*3);
//
//        return rejson(200,'操作成功', $data);
//    }

    public function check_add_user_linshi_key(){
        $auth_token = input('auth_token');
        $backurl = input('backurl');
        $type = input('type');

        $redis = Cache::store('redis');
        $auth_token_val = $redis->get('AUTH_KEY:'.$auth_token);

        // 存在鉴权秘钥，获取openid，然后创建账号
        if($auth_token_val){
            $oauth2_url = request()->domain().'/index.php/html5/weixin/oauth2_add_user?backurl='.$backurl.'&auth_token='.$auth_token.'&type='.$type;
            header('Location: '.$oauth2_url);
        }else{
            echo '<script>alert("参数错误")</script>';die;
        }
    }

    public function uploadImg(): Json
    {
        $fileArr = request()->file("file"); //接收文件
        $file = $_FILES['file']; //得到传输的数据
        $saveName = img($fileArr,$file);
        $data["name"] = $file["name"];
        $data["url"] = '/'.$saveName;

        return rejson(200,'操作成功', $data);
    }

    public function get_add_user_auth_token()
    {
        $auth_token = md5('yikangbao_'.time().'_'.rand(1000000,9999999));

        $redis = Cache::store('redis');
        $redis->set('AUTH_KEY:'.$auth_token, 1,600);

        return rejson(200,'操作成功', ['auth_token'=>$auth_token]);
    }
}
